﻿package {
	import jnBase.Api;
	import jnBase.Data;
	import jnBase.PSteps;
	import jnBase.components.GetRequest;
	import jnBase.view.PageLoading;
	import jnBase.view.SceneToolControl;
	import jnBase.view.SceneToolsWAP;
	
	import laya.net.AtlasInfoManager;
	import laya.net.Loader;
	import laya.net.URL;
	import laya.net.WorkerLoader;
	import laya.utils.Browser;
	import laya.utils.Handler;
	import laya.utils.Stat;
	import laya.utils.Utils;
	
	public class Main {
		public function Main() {
			//根据IDE设置初始化引擎		
			if (window["Laya3D"]) window["Laya3D"].init(GameConfig.width, GameConfig.height);
			else Laya.init(GameConfig.width, GameConfig.height, Laya["WebGL"]);
			Laya["Physics"] && Laya["Physics"].enable();
			Laya["DebugPanel"] && Laya["DebugPanel"].enable();
			Laya.stage.scaleMode = GameConfig.scaleMode;
			Laya.stage.screenMode = GameConfig.screenMode;
			Laya.stage.alignV = GameConfig.alignV;
			Laya.stage.alignH = GameConfig.alignH;
			Laya.stage.bgColor="#000000";
			//兼容微信不支持加载scene后缀场景
			URL.exportSceneToJson = GameConfig.exportSceneToJson;
			
			//打开调试面板（IDE设置调试模式，或者url地址增加debug=true参数，均可打开调试面板）
			if (GameConfig.debug || Utils.getQueryString("debug") == "true") Laya.enableDebugPanel();
			if (GameConfig.physicsDebug && Laya["PhysicsDebugDraw"]) Laya["PhysicsDebugDraw"].enable();
			if (GameConfig.stat) Stat.show();
			Laya.alertGlobalError = false;
			//激活资源版本控制，版本文件由发布功能生成
			// ResourceVersion.enable("version.json", Handler.create(this, this.onVersionLoaded), ResourceVersion.FILENAME_VERSION);
			onVersionLoaded();
		}
		
		private function onVersionLoaded():void {
			//激活大小图映射，加载小图的时候，如果发现小图在大图合集里面，则优先加载大图合集，而不是小图
			AtlasInfoManager.enable("fileconfig.json", Handler.create(this, this.onConfigLoaded));
		}
		private function onConfigLoaded():void {
			Laya.loader.load("shiyanobj.json",Handler.create(this,getObj));
		}
		private function getObj(){
			var data:*=Loader.getRes("shiyanobj.json");
			Data.shiyanData=data;
			Data.HttpRequest=data[5].httpRequest;
			Data.httpurl_expTest=Data.shiyanData[5].baseUrl+Data.shiyanData[5].expTestList_url;
			Data.httpurl_task=Data.shiyanData[5].baseUrl+Data.shiyanData[5].task_url;
			Browser.document.title=Data.shiyanData[0].value;
			
			
			var token:String=GetRequest.getRequests().token;
			if(token){
				Data.token=token;
				//练习
				if(GetRequest.getRequests().practiceid!=undefined){
					Data.PracticeId=GetRequest.getRequests().practiceid; 
					Data.operationType=1;
					Data.isExam=false;
					Data.timeLimit=false;
				}
				//任务
				else if(GetRequest.getRequests().pubtaskid!=undefined){
					Data.PracticeId=GetRequest.getRequests().pubtaskid; 
					Data.operationType=2;
					Data.isExam=true;
					Data.timeLimit=false;
				}
				//如果是实验考核，要先用考核id去获取考核记录id								
				else if(GetRequest.getRequests().operatecheckid!=undefined){
					Data.PracticeId=GetRequest.getRequests().operatecheckid;
					Data.operationType=0;
					Data.isExam=true;
					Data.timeLimit=true;
				}
				Api.GetPracticeById(Data.operationType,Data.PracticeId,this,function(res){
					Data.practiceInfo=res.rows;
					if(Data.operationType==0){
						Data.examTime=res.rows.operatetime;
					}
					this.check();
				})
			}
			else{
				check();
			}
			if(GetRequest.getRequests().urlType!=undefined){
				Data.urlType=GetRequest.getRequests().urlType;
			}
			
		}
		private function check():void{
			//外部配置文件本地调试时设置为false 正式发布到服务器时设置为true
			if(Data.shiyanData[5].yanzheng=="true" && Data.token){
				yanzheng();
				if(Browser.onPC){
					jinyong();
				}
			}
			else{
				begin();
			}
		}
		private var loadprogress:PageLoading;
		private function begin():void
		{
			// TODO Auto Generated method stub
			MyShiyan.getInstance();
			//设置loading界面
			loadprogress=PageLoading.getInstance();
			loadprogress.once("loadReady",this,function():void
			{
				loadingReady();
			})
		}
		private function loadingReady():void
		{
			WorkerLoader.workerPath = "libs/worker.js";
			//开启worker线程
			WorkerLoader.enable = true;
			//加载资源
			Laya.loader.load(Data.assetsList,Handler.create(this,resReady),new Handler(this,progressHandler));
			function progressHandler(e:Number):void
			{
				loadprogress.progress(e);
			}		
		}
		/**
		 *资源预加载完毕 
		 * 
		 */		
		private function resReady():void
		{
			WorkerLoader.enable=false;
			if(Browser.onPC){
				Laya.stage.addChild(SceneToolControl.getInstance());
			}
			else if(Browser.onMobile){
				Laya.stage.addChild(SceneToolsWAP.getInstance());
			}
		}
		/**
		 *禁用F12
		 * 禁用打开控制台 
		 * 
		 */		
		private function jinyong():void{
			var window:*=Browser.window;
			Browser.document.onkeydown = function() {
				if (Browser.window.event && Browser.window.event.keyCode == 123) {
					Browser.window.event.keyCode = 0;
					Browser.window.event.returnValue = false;
					return false;
				}
			};
			resize();
			window.onresize=function(){
				resize();
			};
			function resize(){
				var thca:int = 160; // 打开控制台的宽或高阈值
				if (window.outerWidth - window.innerWidth > thca ||
					window.outerHeight - window.innerHeight > thca) {
					alert("为了不影响您的正常体验，请关掉F12");
					window.location.reload();
				}
			}
		}
		/**
		 *验证用户是否登录 
		 * 
		 */		
		private function yanzheng():void
		{
			Api.GetTokenUser(this,httpComplete)
		}
		/**
		 *请求登录信息 
		 * @param data
		 * 
		 */		
		private function httpComplete(data:Object):void
		{
			if(data.success){
				//学生
				if(data.rows.usertype==6){
					Data.httpurl_info=Data.shiyanData[5].baseUrl+Data.shiyanData[5].stu_infourl;
					Data.httpurl_syfl=Data.shiyanData[5].baseUrl+Data.shiyanData[5].stu_operationurl;
				}
				//老师
				else{
					Data.httpurl_info=Data.shiyanData[5].baseUrl+Data.shiyanData[5].tea_infourl;
					Data.httpurl_syfl=Data.shiyanData[5].baseUrl+Data.shiyanData[5].tea_operationurl;
				}
				begin();
			}
			else{
				Browser.window.location.href = Data.shiyanData[5].baseUrl;
			}
		}
	}
}